package cn.doctorpda.conference.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.bean.doc.DocInfoItem;
import cn.doctorpda.conference.utils.ImageUtils;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.utils.ScreenUtils;

/**
 * Created by huangjinlong on 2016/3/29.
 */
public class InfoAdapter extends RecyclerView.Adapter<InfoAdapter.ViewHolder> {

    //    private List<String> datas = new ArrayList<>();
    private ArrayList<DocInfoItem> infoList = new ArrayList<DocInfoItem>();
    private OnItemClickListener listener;
    private Context context;
    private String isHome = "";

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, DocInfoItem data);
    }

    public InfoAdapter() {

    }

    public InfoAdapter(String isHome) {
        this.isHome = isHome;
    }

    public void clear() {
        this.infoList.clear();
        notifyDataSetChanged();
    }

    public void addAll(ArrayList<DocInfoItem> infoList) {
        this.infoList.addAll(infoList);
        notifyDataSetChanged();
    }

    public void add(String data) {
//        this.datas.add(2, data);
        notifyItemInserted(2);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collection, parent, false);
        LinearLayout.LayoutParams la = new LinearLayout.LayoutParams(ScreenUtils.getScreenWidth(parent.getContext()) / 2, ScreenUtils.getScreenHeight(parent.getContext()) / 4);
        view.setLayoutParams(la);
        this.context = parent.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final int pos = position;
        final DocInfoItem itemInfo = infoList.get(pos);
        if (itemInfo.getTHUMB() != null && !itemInfo.getTHUMB().isEmpty()) {
            ImageUtils.loadImage(context, itemInfo.getTHUMB(), holder.imvMeetingPic);
        } else {
            try {
                if (TextUtils.isEmpty(itemInfo.getTYPE())) {
                    if (itemInfo.getTYPE().equals("video")) {
                        holder.imvMeetingPic.setBackgroundResource(R.drawable.more_shipin);
                    } else if (itemInfo.getTYPE().equals("ppt")) {
                        holder.imvMeetingPic.setBackgroundResource(R.drawable.more_ppt);
                    } else if (itemInfo.getTYPE().equals("document")) {
                        holder.imvMeetingPic.setBackgroundResource(R.drawable.more_wenxian);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (isHome.equals("home")) {
            holder.tvState.setVisibility(View.VISIBLE);
            L.d("首页资料"+itemInfo.getTYPE());
            try {
                if (!TextUtils.isEmpty(itemInfo.getTYPE())) {
                    if (itemInfo.getTYPE().equals("video")) {
                        holder.tvState.setText("视频");
                    } else if (itemInfo.getTYPE().equals("ppt")) {
                        holder.tvState.setText("PPT");
                    } else if (itemInfo.getTYPE().equals("document")) {
                        holder.tvState.setText("文献");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            holder.tvState.setVisibility(View.INVISIBLE);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String date_start = "", date_stop = "";
        if (!TextUtils.isEmpty(itemInfo.getCONTRIBUTE_AT())) {
            try {
                date_start = sdf.format(Date.valueOf(itemInfo.getCONTRIBUTE_AT().substring(0, 9)));
//                holder.tvMeetingDate.setText(date_start);
            } catch (Exception e) {
                e.printStackTrace();
                holder.tvSource.setText("待确定");
            }
        } else {
            holder.tvSource.setText("待确定");
        }

        holder.tvAuthor.setText(itemInfo.getAUTHOR());
        holder.tvSource.setText(date_start);
        holder.tvUnit.setText(itemInfo.getCOMPANY());
        holder.tvTitle.setText(itemInfo.getTITLE());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position, infoList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (isHome.equals("home")) {
            if (infoList.size() > 2) {
                return 2;
            }
        }
        return infoList.size();
    }

    //item元素
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle, tvUnit, tvAuthor, tvSource, tvState, tvSource_n;//会议标题，会议状态，会议时间，会议地址,状态
        ImageView imvMeetingPic;//会议图片

        public ViewHolder(View itemView) {
            super(itemView);
            tvState = (TextView) itemView.findViewById(R.id.txt_item_state);
            tvState.setVisibility(View.INVISIBLE);
            tvTitle = (TextView) itemView.findViewById(R.id.txt_item_title);
            tvAuthor = (TextView) itemView.findViewById(R.id.txt_item_author);
            tvUnit = (TextView) itemView.findViewById(R.id.txt_item_unit);
            tvSource = (TextView) itemView.findViewById(R.id.txt_item_source);
            tvSource_n = (TextView) itemView.findViewById(R.id.txt_source);
            imvMeetingPic = (ImageView) itemView.findViewById(R.id.imv_meeting);
        }
    }
}
